Skip to content

Conversation

@erdtsieck
Copy link
Contributor

This change addresses #3896 , where duplicating fields from subclasses (Duplicate<TSub>) resulted in unconditional casts in the generated Upsert, Insert, and Update operations.
When saving instances of the base type, this caused an InvalidCastException.

Changes include:

  • Updated UpsertArgument.GenerateCodeToSetDbParameterValue to perform safe type checks before casting to the subclass.
  • Applied the same safe access pattern in GenerateBulkWriterCodeAsync to avoid runtime cast errors.
  • Extended use of AccessorString with guards for subtype casting.
  • Added XML documentation for both Duplicate<TSub> overloads in DocumentMappingExpression and DocumentMapping<T> to clarify API intent.

Impact:

  • Subclass fields can now be safely marked as duplicated fields without causing runtime cast exceptions.
  • Generated storage code works correctly for mixed collections of base type and subclass instances.
  • API documentation now clearly describes the usage and parameters of Duplicate<TSub>.

…nerated storage code (JasperFx#3896)

This change addresses JasperFx#3896, where duplicating fields from subclasses (`Duplicate<TSub>`) resulted in unconditional casts in the generated `Upsert`, `Insert`, and `Update` operations.
When saving instances of the base type, this caused an `InvalidCastException`.

Changes include:

* Updated `UpsertArgument.GenerateCodeToSetDbParameterValue` to perform safe type checks before casting to the subclass.
* Applied the same safe access pattern in `GenerateBulkWriterCodeAsync` to avoid runtime cast errors.
* Extended use of `AccessorString` with guards for subtype casting.
* Added XML documentation for both `Duplicate<TSub>` overloads in `DocumentMappingExpression` and `DocumentMapping<T>` to clarify API intent.

Impact:

* Subclass fields can now be safely marked as duplicated fields without causing runtime cast exceptions.
* Generated storage code works correctly for mixed collections of base type and subclass instances.
* API documentation now clearly describes the usage and parameters of `Duplicate<TSub>`.
CS0411: The type arguments for method 'IGroupedParameterBuilder.AppendParameter<T>(T?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Ensure generated storage operations use the underlying value type (e.g. string, Guid) when accessing .Value on strongly-typed IDs within conditional expressions.
This prevents CS1503 compiler errors caused by mismatched types between true/false branches and allows correct parameter binding for nullable and non-nullable strong-typed IDs.
@jeremydmiller jeremydmiller merged commit 4b2c404 into JasperFx:master Aug 19, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants